home *** CD-ROM | disk | FTP | other *** search
- Path: goanna.cs.rmit.EDU.AU!not-for-mail
- From: ok@goanna.cs.rmit.EDU.AU (Richard A. O'Keefe)
- Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c
- Subject: Re: Hungarian notation - whoops!
- Date: 1 Mar 1996 20:34:49 +1100
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Message-ID: <4h6gbp$guh@goanna.cs.rmit.EDU.AU>
- References: <30C40F77.53B5@swsbbs.com> <4fc157$jsf@goanna.cs.rmit.EDU.AU> <dewar.823793746@schonberg> <4fms62$c0p@goanna.cs.rmit.EDU.AU> <4ft1ruINN6dr@keats.ugrad.cs.ubc.ca> <4g9255$74s@goanna.cs.rmit.EDU.AU> <824909942snz@genesis.demon.co.uk>
- NNTP-Posting-Host: goanna.cs.rmit.edu.au
- X-Newsreader: NN version 6.5.0 #0 (NOV)
-
- I complain about abs(X) giving wrong answers.
-
- Lawrence Kirby <fred@genesis.demon.co.uk> writes:
-
- >Checking against overflow is required whatever representation is used.
-
- In the case of unary minus and abs, this is not true.
- Those are the functions I was discussing.
-
- >Once this is established using 2's complement is no worse than any other
- >representation. Did you see my earlier reply in the thread on this subject?
-
- Yes, but it was neither comfort nor assistance.
-
- >If nothing else it seems to me that 2's complement is a big win with
- >regards to implementing multiple-precision arithmetic since lower order
- >bits of the result of most operations don't depend on the signs of the
- >operands or the sign of the result.
-
- I have implemented multiple-precision arithmetic, and I must say I found
- 2s-complement a confounded nuisance. What I wanted, and what I used,
- was arithmetic on N-bit natural numbers with overflow. In short, what
- I wanted for multiple precision arithmetic was
- N-bit natural + N-bit natural -> N-bit natural + carry
- N-bit natural - N-bit natural -> N-bit natural + borrow
- N-bit natural x N-bit natural -> 2N-bit natural
- 2N-bit natural (/,%) N-bit natural -> N-bit natural, N-bit natural
- Interpreting any of these as signed gave rise to serious complications.
-
- Oh dear. I'm beginning to sound like Hermann Rubin.
-
- >>The practical consequence of this is that responsibility for ensuring that
- >>the input to unary minus or absolute value is in range is passed onto the
- >>programmer. It is that which I am complaining about. It doesn't make my
- >>life as a programmer one teeny tiny bit easier.
-
- >It doesn't make it any harder either - see my earlier post.
-
- I saw your earlier post all right, and I didn't agree with it.
- Perhaps in theory you might be right. But in PRACTICE, it isn't true.
- I have, for example, seen Pascal compilers where addition, subtraction,
- and multiplication were checked for overflow, but unary minus, absolute
- value, and division, were NOT checked for overflow, because they "obviously"
- couldn't. But they can.
-
- (Against myself, I must admit that one Pascal compiler for SPARCs did check
- the overflow flag after multiplication, but the implementor hadn't bothered
- to check what the multiplication routine actually _did_: Sun's code always
- cleared the overflow bit whatever happened. Ouch!)
-
- >>Modular arithmetic in Ada 95 *is* the ticket for portable hackery.
- >>Unsigned arithmetic in C is indeed defined to be modulo 2**n for some
- >>n, but the bounds on n are very very loose, and the price of portability
- >>is much programmer-inserted masking.
-
- >Well, a small amount of masking.
-
- Every operation except comparison needs it. That's not a small amount.
-
- >True, modulo arithmetic isn't useful in contexts where it simply generates
- >the wrong result (however it does make it easy to test for overflow after
- >an unsigned operation).
-
- It depends on what you mean by easy. There are machines where the unsigned
- arithmetic operations don't set the carry/borrow/overflow/whatever bit.
- There are others where there are minor glitches, e.g. on the M88100,
- unsigned x, y;
- x += y <<without overflow check>>
- x += y <<with overflow check>>
- x += 1 <<without overflow check>>
- all take one instruction, but
- x += 1 <<with overflow check>>
- takes two instructions, and the integer multiply instruction delivers the
- bottom 32 bits with no overflow indication available at all.
-
- I suspect that you mean that it is easy in C using unsigned arithmetic
- to test for overflow by using a short sequence of instructions. I am
- well aware of that, and I am also aware that it isn't easy, although C++
- or Ada could let you do this without cluttering up the visual appearance
- of the code (user-overloaded inline operators).
-
- >Only if the code doesn't test for wrap to zero. Unsigned types make this
- >test very easy.
-
- YES BUT THEY MAKE IT ******NECESSARY*******.
- (and in C, they don't make it any easier than signed types, in practice).
-
- The whole point that I am making is that MY code gets cluttered up!
-
- >>Overflows aren't the problem. Restricted machine arithmetic is the problem.
-
- >It depends on where you put the limit. Language support for multiple-precision
- >arithmetic or specified precision beyond 64 bits (or possibly 128 bits)
- >has some uses but they are rare across the whole spectrum of code.
-
- I haven't been asking for multiple precision arithmetic.
- I haven't been asking for specified precision beyond 64 bits.
- (In my computing youth, I had 78 bits + sign and was happy. Sigh.)
- All I keep on saying is that
- - hardware arithmetic which delivers "wrong" results
- - combined with compilers that don't check the warning flags
- that ARE available in many architectures
- push a lot of implementation complexity into MY code that doesn't belong
- there, and I don't like it.
-
- I am advocating Dijkstra's notion of a "Hopefully sufficiently large machine",
- where you write a clean program that says what you mean, and if the software
- and hardware combination can't do that, you certainly get told.
-
- >>You are confusing *unaligned* access with *byte* access. Like it or not, it
- >>is a fact that the machines I was talking about DID support byte access,
- >>DID support packed arrays of character, but did NOT support unaligned
- >>access.
-
- >And there are yet other systems (e.g. Crays) which don't support byte access
- >but compilers can still support characters arrays where the character unit
- >is smaller than the machine word unit by bit twiddling tricks.
-
- I know that. The CDC machines Pascal ran on were just such machines.
- You missed the point.
- --
- Election time; but how to get Labor _out_ without letting Liberal _in_?
- Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
-